package com.ruoyi.task.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.appuser.domain.Appuser;
import com.ruoyi.appuser.service.IAppuserService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.season.domain.PlaySeasonRecord;
import com.ruoyi.season.service.IPlaySeasonRecordService;
import com.ruoyi.util.DateUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author mjl
 */
@RestController
@Api(value = "定时任务", tags = "定时任务接口")

public class TaskController {

    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
    @Resource
    private IAppuserService appuserService;
    @Resource
    private IPlaySeasonRecordService playSeasonRecordService;



    /**
     * 赛季与会员天数相同，到期后用户会员积分清零
     * 会员有效期三十天
     * 每天零点检验会员是否过期
     */
    @Scheduled(cron = "0 0 0 * * ?")
//    /**每分钟执行一次*/
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void checkAppuserVipInfo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String todayDate = sdf.format(date);
        List<Appuser> appuserList = appuserService.list(new LambdaQueryWrapper<Appuser>()
                .eq(Appuser::getIsVip,2)
                .eq(Appuser::getIsDeleted,0));
        if(!CollectionUtils.isEmpty(appuserList))
        {
             for(Appuser temp : appuserList)
             {
                 String vipTime = sdf.format(DateUtils.getTime(temp.getVipTime(), 30));
                 if(todayDate.compareTo(vipTime) == 1)
                 {
                     PlaySeasonRecord normalInfo = playSeasonRecordService.getOne(
                             new LambdaQueryWrapper<PlaySeasonRecord>()
                             .eq(PlaySeasonRecord::getType,1)
                             .eq(PlaySeasonRecord::getAppuserId,temp.getId())
                     );

                     if(!playSeasonRecordService.update(new LambdaUpdateWrapper<PlaySeasonRecord>()
                             .set(PlaySeasonRecord::getStatus,1)
                             .eq(PlaySeasonRecord::getAppuserId,temp.getId())
                             .ne(PlaySeasonRecord::getStatus,3)
                     )){
                         logger.error("更新赛季状态失败");

                     }

                     if(!playSeasonRecordService.update(new LambdaUpdateWrapper<PlaySeasonRecord>()
                             .set(PlaySeasonRecord::getStatus,2)
                             .eq(PlaySeasonRecord::getAppuserId,temp.getId())
                             .eq(PlaySeasonRecord::getId,normalInfo.getId())
                     )){
                         logger.error("更新赛季进行中失败");

                     }


                     if(!appuserService.update(new LambdaUpdateWrapper<Appuser>()
                             .set(Appuser :: getIsVip,1)
                             .set(Appuser :: getUpdateTime,date)
                             .set(Appuser :: getVipIntegral,new BigDecimal(0))
                             .set(Appuser :: getPlaySeasonRecordId, normalInfo.getId())
                             .eq(Appuser :: getId,temp.getId())
                     )) {

                         logger.error("更新会员失败,用户id:"+temp.getId());
                     }


                 }
              }
        }

    }
}
